home *** CD-ROM | disk | FTP | other *** search
- unit Icnflbox;
-
- interface
-
- uses
- SysUtils, WinTypes, WinProcs, Messages, Classes, Graphics, Controls,
- Forms, Dialogs, StdCtrls;
-
- type
- TIconsAndFilesListbox = class(TFileListbox)
- public
- { public methods and data }
- procedure ReadFileNames; override;
- end;
-
- procedure Register;
-
- implementation
-
- { Modified from VCL Source Copyright 1995 }
- { Borland International, Inc. }
- { Use this to override display with icons }
- procedure TIconsAndFilesListBox.ReadFileNames;
- { This procedure gets an icon for a file using FindExecutable }
- { and ExtractIcon. (assumes file/dir is passed) }
- procedure GetIconForFile( TheName : String; var TheIcon : TIcon );
- var TheExt : String; { File extension holder }
- TheOtherPChar , { Windows ASCIIZ string }
- ThePChar : PChar; { Windows ASCIIZ string }
- Dummy : Word;
- begin
- { Check for directory and if so get directory icon from RES file }
- if (( FileGetAttr( TheName ) and faDirectory ) = faDirectory ) then
- begin
- { Set up the PChar to communicate with Windows }
- GetMem( TheOtherPChar , 255 );
- { Convert Pascal-style string to ASCIIZ Pchar }
- StrPCopy( TheOtherPChar , 'DIRECTORY' );
- { Use API call to return icon handle of Icon Resource in FILECTRL.RES }
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- { Release memory from PChar }
- FreeMem( TheOtherPChar , 255 );
- { Leave }
- exit;
- end;
- { Assume archive file; get its extension }
- TheExt := Uppercase( ExtractFileExt( TheName ));
- { If not an executable/image file then use FindExecutable to get icon }
- if (( TheExt <> '.EXE' ) and ( TheExt <> '.BAT' ) and
- ( TheExt <> '.PIF' ) and ( TheExt <> '.COM' )) then
- begin
- { Grab three chunks of memory }
- GetMem( ThePChar , 255 );
- { Set up the name and its directory in Windows string formats }
- StrPCopy( ThePChar, TheName );
- Dummy := 65535;
- {**** Windows 95 Specialized call ****** }
- TheIcon.Handle := ExtractAssociatedIcon( hInstance , ThePChar , Dummy );
- if TheIcon.Handle = 0 then
- begin
- GetMem( TheOtherPChar , 255 );
- StrPCopy( TheOtherPChar , 'NOICON' );
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- FreeMem( TheOtherPChar , 255 );
- exit;
- end;
- FreeMem( ThePChar , 255 );
- end
- else
- { Assume Windows Executable file, so get icon from it with ExtractIcon API }
- begin
- GetMem( ThePChar , 255 );
- StrPCopy( ThePChar , TheName );
- { Try to get first icon for file }
- Dummy := 65535;
- TheIcon.Handle := ExtractAssociatedIcon( hInstance , ThePChar , Dummy );
- FreeMem( ThePChar , 255 );
- { If handle is 0 invalid icon format so use default from RES file }
- if TheIcon.Handle = 0 then
- begin
- GetMem( TheOtherPChar , 255 );
- StrPCopy( TheOtherPChar , 'NOICON' );
- TheIcon.Handle := LoadIcon( hInstance , TheOtherPChar );
- FreeMem( TheOtherPChar , 255 );
- exit;
- end;
- end;
- end;
-
- var
- AttrIndex : TFileAttr;
- i : Integer;
- FileExt : string;
- MaskPtr : PChar;
- Ptr : PChar;
- AttrWord : Word;
- TempPicture : TPicture;
- TempBmp : TBitmap;
- TempIcon : TIcon;
- const
- Attributes: array[TFileAttr] of Word =
- ( DDL_READONLY , DDL_HIDDEN , DDL_SYSTEM , $0008 , DDL_DIRECTORY ,
- DDL_ARCHIVE , DDL_EXCLUSIVE );
- begin
- { if no handle allocated yet, this call will force }
- { one to be allocated incorrectly (i.e. at the wrong time. }
- { In due time, one will be allocated appropriately. }
- AttrWord := DDL_READWRITE;
- if HandleAllocated then
- begin
- { Set attribute flags based on values in FileType }
- for AttrIndex := ftReadOnly to ftArchive do
- if AttrIndex in FileType then
- AttrWord := AttrWord or Attributes[ AttrIndex ];
-
- { Use Exclusive bit to exclude normal files }
- if not ( ftNormal in FileType ) then
- AttrWord := AttrWord or DDL_EXCLUSIVE;
-
- ChDir( FDirectory ); { go to the directory we want }
- Clear; { clear the list }
-
- GetMem( MaskPtr , 256 );
- StrPCopy( MaskPtr , FMask );
- while MaskPtr <> nil do
- begin
- Ptr := StrScan ( MaskPtr , ';' );
- if Ptr <> nil then Ptr^ := #0;
- { build the list }
- SendMessage( Handle , LB_DIR , AttrWord , Longint( MaskPtr ));
- if Ptr <> nil then
- begin
- Ptr^ := ';';
- Inc ( Ptr );
- end;
- MaskPtr := Ptr;
- end;
- FreeMem( MaskPtr , 256 );
- { Now add the bitmaps }
- {---------------------------- begin custom code --------------------------}
- { Create the TPicture for exchange purposes }
- TempPicture := TPicture.Create;
- { Set it to icon widths }
- TempPicture.Bitmap.Width := 32;
- TempPicture.Bitmap.Height := 32;
- { Run down the list }
- for i := 0 to Items.Count - 1 do
- begin
- { Create a New temporary icon }
- TempIcon := TIcon.Create;
- { Call the custom DRWS routine to get icon for a file }
- GetIconForFile( Items[ i ] , TempIcon );
- { Put the icon on the bitmap for the picture via draw }
- { Note 1 , 1 due to bug in Draw? }
- TempPicture.Bitmap.Canvas.Draw( 1 , 1 , TempIcon );
- { Create a temporary bitmap }
- TempBmp := TBitmap.Create;
- { Set its width to those of the previous object's bitmaps }
- TempBmp.Width := 16;
- TempBmp.Height := 15;
- { Resize the icon's bitmap to the smaller size with stretchdraw }
- TempBmp.Canvas.StretchDraw( Rect( 1 , 1 , 15 , 14 ) ,
- TempPicture.Bitmap );
- { Set the Objects list to the bitmap }
- Items.Objects[ i ] := TempBmp;
- { Free the icon each iteration; don't free the TempBmp as list does }
- TempIcon.Free;
- end;
- { Free the TPicture exchange element }
- TempPicture.Free;
- {------------------------ end custom code --------------------------------}
- Change;
- end;
- end;
-
- procedure Register;
- begin
- RegisterComponents('Widgets', [TIconsAndFilesListbox]);
- end;
-
- end.
-